package com.book.filter;

import com.book.entity.User;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
 **@Author:youpeng
 **Created on 2022/4/24
 */
@WebFilter("/*")
public class MainFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        String url = req.getRequestURL().toString();
        if(!url.contains("/static/")&&!url.endsWith("login")){
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute("user");
            if(user==null&&!url.endsWith("register")){
                res.sendRedirect("login");
                return;
            }
        }
        chain.doFilter(req,res);
    }
}
